import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Stream;

public class ZipList {
    

    public static void main(String[] args) {
        
        try{

            Scanner scanner = new Scanner(System.in);

            String originStr = scanner.nextLine();
            String origin = scanner.nextLine();

            List<List<String>> realTarget = new ArrayList<>();

            //按]拆分
            String[] originOne = origin.split("\\]");

            for(int i=0; i<originOne.length; i++){

                String originSub = originOne[i];

                //按照[ 拆分

                List<String> originSubList = new ArrayList<>();

                String[] originSubOne = originSub.split("\\[");
                String normalStr = originSubOne[0];

                for(int j=0; j<originSubOne[1].length(); j++){

                    StringBuilder builder = new StringBuilder();
                    builder.append(normalStr);
                    builder.append(originSubOne[1].charAt(j));

                    originSubList.add(builder.toString());
                }


                realTarget.add(originSubList);
            }

            //realTarget.stream().forEach(System.out::println);

            List<String> realZip = new ArrayList<>();

            for(int i=0; i<realTarget.size(); i++){

                zipList(realZip, realTarget.get(i));

            }
            // for(int i=0; i<realTarget.get(0).size(); i++){
            //     for(int j=0; j<realTarget.get(1).size(); j++){
            //         for(int m=0; m<realTarget.get(2).size(); m++){
            //             realZip.add(realTarget.get(0).get(i)
            //             +realTarget.get(1).get(j)
            //             +realTarget.get(2).get(m));

            //         }
            //     }


            // }

            //realZip.stream().forEach(System.out::println);

            int rightIndex = -1;

            for(int i=0; i<realZip.size(); i++){

                if(originStr.contains(realZip.get(i))){

                    rightIndex = originStr.indexOf(realZip.get(i));
                }
            }


            System.out.println(rightIndex);



        }catch(Exception e){
            e.printStackTrace();
        }
    }

    private static void zipList(List<String> realZip, List<String> mergeList){

        if(realZip.isEmpty()){
            realZip.addAll(mergeList);
        }else{

            List<String> temps = new ArrayList<>();

            for(int k=0; k<realZip.size(); k++){
                for(int m=0; m<mergeList.size(); m++){
    
                    temps.add(realZip.get(k)+mergeList.get(m));
                }
            }
            realZip.clear();
            realZip.addAll(temps);
        }

    }
}
